<?php
/**
 * Created by PhpStorm.
 * User: wuxiaojie
 * Date: 2018/7/22
 * Time: 20:25
 */


    define('IN_AJAX', TRUE);


if (!defined('IN_ANWSION'))
{
    die;
}
class aomas_site_ajax extends AWS_ADMIN_CONTROLLER
{
    public function setup()
    {
        HTTP::no_cache_header();
    }

    public function add_nav_menu_action()
    {
        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }

        switch ($_POST['type'])
        {
            case 'category' :
                $type_id = intval($_POST['type_id']);
                $category = $this->model('system')->get_category_info($type_id);
                $title = $category['title'];
                break;

            case 'custom' :
                $key = trim($_POST['class_name']);
                $title = trim($_POST['title']);
                $description = trim($_POST['description']);
                $link = trim($_POST['link']);
                $type_id = 0;
                break;
        }

        if (!$key)
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择第几行')));
        }
        if (!$title)
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入导航标题')));
        }

        $this->model('menu')->add_aomas_nav_menu($key,$title, $description, $_POST['type'], $type_id, $link);

        H::ajax_json_output(AWS_APP::RSM(null, 1, null));
    }


    public function save_nav_menu_action()
    {
        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }

        $aomas_nav = $this->model('menu')->get_aomas_nav_menu_list();
        $arr_tpl = array();
        $class_name = trim($_POST['class_name']);
        $current_nav   =  $aomas_nav[$class_name];



        if ($_POST['nav_menu'])
        {
            foreach($_POST['nav_menu'] as $key => $val)
            {

                $arr_tpl[]= $val;
//                $this->model('menu')->update_nav_menu($key, $val);
            }
        }
        if(count($arr_tpl)>0){
            $aomas_nav[$class_name] = $arr_tpl;
            $this->model('menu')->update_aomas_nav_menu($aomas_nav);
        }
        H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('导航菜单保存成功')));
    }

    /**
     * 首页设置
     */
    public function save_home_data_action()
    {

        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }
        $home_data = (array)AWS_APP::config()->get('home_data_v1');

         $home_data[0] = $_POST['home_data'];

        AWS_APP::config()->set('home_data_v1',$home_data);


        H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('导航菜单保存成功')));
    }
    /**
     * wap 首页设置
     */
    public function save_wap_home_data_action()
    {

        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }
        $home_data = (array)AWS_APP::config()->get('home_data_v1');


        $home_data[1] = $_POST['home_data'];
        AWS_APP::config()->set('home_data_v1',$home_data);


        H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('导航菜单保存成功')));
    }

}